#!/bin/bash

ZSH="$HOME/.oh-my-zsh"
ZSHRC="$HOME/.zshrc"

SUDO="sudo "
if [[ $(id -u) -eq 0 ]]; then SUDO=""; fi

if [[ -d $ZSH ]]; then
    read -p "zsh is installed, reinstall it? (y/N) " reinstall </dev/tty
    reinstall=$(echo "$reinstall" | tr -s '[:space:]')
    if [[ "$reinstall" != [Yy] ]]; then exit 0; fi
fi

$SUDO rm -rf $ZSH $ZSHRC
$SUDO apt install zsh -y
sh -c "REMOTE=https://gitee.com/wyanlord/ohmyzsh CHSH=no RUNZSH=no $(curl -fsSL https://gitee.com/wyanlord/oh-my-zsh/raw/master/tools/install.sh)"

if [ ! -d $ZSH ] || [ ! -f $ZSHRC ]; then echo "zsh is failured to install"; exit 1; fi

sed -i s/plugins=\(git\)/plugins=\(git\ zsh-256color\ zsh-autosuggestions\)/g $ZSHRC
cat << "EOF" | tee -a $ZSHRC
zstyle ':omz:update' mode disabled
source $ZSH/plugins/zsh-256color/zsh-256color.plugin.zsh
source $ZSH/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
function hproxy { export http_proxy=127.0.0.1:7890; export https_proxy=127.0.0.1:7890; }
function gproxy { git config --global http.proxy "http://127.0.0.1:7890"; git config --global https.proxy "http://127.0.0.1:7890"; }
function noproxy { export http_proxy=; git config --global --unset http.proxy; export https_proxy=; git config --global --unset https.proxy; }
function zadd { curl -fsSL https://gitee.com/wyanlord/sh/raw/master/linux/$1.sh | bash }
EOF

if [[ ! -d $ZSH/plugins/zsh-256color ]]; then
  git clone https://gitee.com/wyanlord/zsh-256color.git $ZSH/plugins/zsh-256color
  if [[ ! -d $ZSH/plugins/zsh-256color ]]; then echo "zsh-256color is failured to install"; exit 1; fi
fi

if [[ ! -d $ZSH/plugins/zsh-autosuggestions ]]; then
  git clone https://gitee.com/wyanlord/zsh-autosuggestions.git $ZSH/plugins/zsh-autosuggestions
  if [[ ! -d $ZSH/plugins/zsh-autosuggestions ]]; then echo "zsh-autosuggestions is failured to install"; exit 1; fi
fi

chsh -s /bin/zsh < /dev/tty
/bin/zsh
